<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <script src="../jsLib/angular-1.3.0.js"></script>
    <title></title>
</head>
<body ng-app>
<div ng-controller="CartController">
    <div ng-repeat="item in items">
        <span>{{item.title}}</span>
        <input ng-model="item.quantity">
        <span>单价：{{item.price | currency}}</span>
        <span>总共：{{item.price * item.quantity | currency}}</span>
    </div>
    <div>总共: {{totalCart | currency}}</div>
    <div>打折: {{bill.discount | currency}}</div>
    <div>实收: {{subtotal | currency}}</div>
</div>
<script>
    /*   function CartController($scope){
     $scope.bill = {};

     $scope.items = [
     {title: 'Paint pots', quantity: 8, price: 3.95},
     {title: 'Polka dots', quantity: 17, price: 12.95},
     {title: 'Pebbles', quantity: 5, price: 6.95}
     ];

     $scope.totalCart = function(){
     alert(1)
     var total = 0;
     for (var i = 0, len = $scope.items.length; i < len; i++){
     total = total + $scope.items[i].price* $scope.items[i].quantity;
     }

     return total;
     };

     $scope.subtotal = function(){
     return $scope.totalCart() - $scope.bill.discount;
     };
     function calculateDiscount(newValue, oldValue, scope){
     alert(newValue)
     $scope.bill.discount = newValue > 100 ? 10 : 0;
     }

     $scope.$watch($scope.totalCart, calculateDiscount);

     }*/

    function CartController($scope){
        $scope.bill = {};

        $scope.items = [
            {title: 'Paint pots', quantity: 8, price: 3.95},
            {title: 'Polka dots', quantity: 17, price: 12.95},
            {title: 'Pebbles', quantity: 5, price: 6.95}
        ];




        function calculateDiscount(newValue, oldValue, scope){
            alert(11)
            var total = 0;
            for (var i = 0, len = $scope.items.length; i < len; i++){
                total = total + $scope.items[i].price* $scope.items[i].quantity;
            }

            $scope.totalCart=total;
            $scope.bill.discount = total > 100 ? 10 : 0;
            $scope.subtotal =$scope.totalCart-$scope.bill.discount
        }

        $scope.$watch('items', calculateDiscount,true);
    }
</script>
</body>
</html>